package com.senven.books.handler;

import com.alibaba.fastjson.JSON;
import com.senven.books.utils.ResultEnum;
import com.senven.books.utils.ResultVO;
import com.senven.books.utils.ResultVOUtil;
import com.senven.books.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : longshengyi
 * @description :
 * @createDate : 2024/7/22 0:04
 */
@Component
public class AuthenticationEntryPointImpl  implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResultVO resultVO = ResultVOUtil.error(HttpStatus.UNAUTHORIZED.value(), "用户认证失败，请重新登录");
        String json = JSON.toJSONString(resultVO);
        //WebUtils是我们在utils目录写好的类
        WebUtils.renderString(response,json);
    }
}
